package com.syt.hospital.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syt.client.cmn.DictFeignClient;
import com.syt.enums.DictEnum;
import com.syt.hospital.repository.HospitalRepository;
import com.syt.hospital.service.HospitalService;
import com.syt.model.hospital.Hospital;
import com.syt.model.hospital.HospitalSet;
import com.syt.vo.hospital.HospitalQueryVo;
import com.syt.vo.order.SignInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: xxx
 * @Date: 2023/2/9 20:10
 * @Description:
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;



    /**
     * 获取医院名称
     * @param hoscode
     * @return
     */
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(hospital != null) {
            return hospital.getHosname();
        }
        return "";
    }

    /**
     * 根据医院编号获取医院预约挂号详情
     * @param hoscode
     * @return
     */
    @Override
    public Map<String, Object> getHospitalRegisteredByHoscode(String hoscode) {
        Map<String, Object> result = new HashMap<String, Object>();
        //预约挂号详情
        Hospital hospital = this.setHospitalHospitalType(this.getHospitalByHoscode(hoscode));
        result.put("hospital",hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    /**
     * 根据医院名称模糊查询
     * @param hosname
     * @return
     */
    @Override
    public List<Hospital> findByHospName(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    /**
     * 医院详情信息
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> showHosDetail(String id) {
        Map<String, Object> result = new HashMap<>();
        Hospital hospital = this.setHospitalHospitalType(hospitalRepository.findById(id).get());
        //医院基本信息(包含医院等级)
        result.put("hospital",hospital);
        //单处处理更加直观
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    /**
     * 更新医院上线状态
     * @param id
     * @param status
     */
    @Override
    public void updateHospStatus(String id, int status) {
       //根据id查询医院信息
        Hospital hospital = hospitalRepository.findById(id).get();
        //修改设置的值
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    /**
     * 查询医院测试
     * @return
     */
    @Override
    public Object findList() {
        return hospitalRepository.findAll();
    }

    /**
     * 医院列表
     * @param page
     * @param limit
     * @param hospitalQueryVo
     * @return
     */
    @Override
    public Page<Hospital> selectHospPage(Integer page,Integer limit, HospitalQueryVo hospitalQueryVo) {
        //排序
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");

        //创建pageable对象
        PageRequest pageable = PageRequest.of(page-1, limit,sort);

        //创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        //转换
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);

        //创建对象
        Example<Hospital> example = Example.of(hospital, matcher);

        //实现查询
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        //获取list集合，遍历进行医院等级封装
        pages.getContent().stream().forEach(item -> {
            this.setHospitalHospitalType(item);
        });


        return pages;
    }

    //获取list集合,遍历进行医院等级封装
    private Hospital setHospitalHospitalType(Hospital hospital) {
        //根据dictCode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(), hospital.getHostype());
        //查询省 市 区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        //封装
        hospital.getParam().put("hostypeString", hostypeString);
        hospital.getParam().put("fullAddress", provinceString + cityString + districtString + hospital.getAddress());

        return hospital;
    }

    /**
     * 调用service方法实现根据医院编号查询
     * @param hoscode
     * @return
     */
    @Override
    public Hospital getHospitalByHoscode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    /**
     * 上传医院接口
     *
     * @param paramMap
     */
    @Transactional
    @Override
    public void saveHospital(Map<String, Object> paramMap) {
        //1、把参数map集合转成对象Hospital
        String mapString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);

        //判断是否存在数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);

        //如果存在就进行修改
        if (hospitalExist!=null){
            hospitalExist.setStatus(hospitalExist.getStatus());
            hospitalExist.setCreateTime(hospitalExist.getCreateTime());
            hospitalExist.setUpdateTime(new Date());
            hospitalExist.setIsDeleted(0);
            hospitalRepository.save(hospitalExist);
        }else {//如果不存在就添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.insert(hospital);
        }






    }
}
